package com.example.myone.Pager;

import static android.content.ContentValues.TAG;

import android.content.Context;
import android.os.Build;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myone.Base.BasePager;
import com.example.myone.Adapter.HomeArticleAdapter;
import com.example.myone.Entity.Article;
import com.example.myone.Mapper.ArticleMapper;
import com.example.myone.R;
import com.example.myone.Util.DateTimeUtil;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

@RequiresApi(api = Build.VERSION_CODES.O)

public class HomePager extends BasePager {
    private RecyclerView recyclerView;
    private HomeArticleAdapter homeArticleAdapter;
    private RecyclerView.LayoutManager linearLayoutManager;
    private RecyclerView.LayoutManager gridLayoutManager;
    public static boolean isLinear = true;
    TextView date, day,toToday,weather;
    ImageView arrow;


    public HomePager(Context context) {
        super(context);
    }

    @Override
    public View initView() {
        Log.d(TAG, "==> HomePager initView");
        View view = LayoutInflater.from(context).inflate(R.layout.home_pager, null);
        recyclerView = view.findViewById(R.id.recyclerView);
        // 创建布局管理器
        linearLayoutManager = new LinearLayoutManager(context); // 线性布局
        gridLayoutManager = new GridLayoutManager(context, 2); // 2列的网格布局
        date = view.findViewById(R.id.home_top_date);
        day = view.findViewById(R.id.home_top_day);
        toToday = view.findViewById(R.id.toToday);
        weather = view.findViewById(R.id.weather);
        setDayText(date, day, null);
        // 箭头点击事件
        arrow = view.findViewById(R.id.home_top_arrow);

        // 今天点击事件
        toToday.setOnClickListener(v -> {
            HomePager.isLinear = true;
            recyclerView.setLayoutManager(linearLayoutManager);
            homeArticleAdapter.setLayoutType(DateTimeUtil.getNowStr());
            arrow.setRotation(0);
            setDayText(date, day, null);
            change2Today(toToday, weather, DateTimeUtil.getNowStr());
        });
        // 日期点击事件
        LinearLayout homeTopClick = view.findViewById(R.id.home_top_click);
        homeTopClick.setOnClickListener(v -> {
            changeLayoutManager(arrow, null);
        });

        return view;
    }

    private static void change2Today(TextView toToday, TextView weather, String createTime) {
        if (!DateTimeUtil.isToday(createTime)) {
            toToday.setVisibility(View.VISIBLE);
            weather.setVisibility(View.GONE);
        } else {
            toToday.setVisibility(View.GONE);
            weather.setVisibility(View.VISIBLE);
        }
    }

    private static void setDayText(TextView date, TextView day, LocalDate localDate) {
        if (localDate == null) {
            date.setText(String.format("%s.%s", DateTimeUtil.getNowMonth(), DateTimeUtil.getNowYear()));
            day.setText(String.format("%s", DateTimeUtil.getNowDay()));
        } else {
            date.setText(String.format("%s.%s", DateTimeUtil.getMonth(localDate), DateTimeUtil.getYear(localDate)));
            day.setText(String.format("%s", DateTimeUtil.getDay(localDate)));
        }
    }

    private void changeLayoutManager(ImageView arrow, String createTime) {
        HomePager.isLinear = !HomePager.isLinear;
        recyclerView.setLayoutManager(HomePager.isLinear ? linearLayoutManager : gridLayoutManager);
        homeArticleAdapter.setLayoutType(createTime);
        arrow.animate().rotationBy(180f).start();
    }

    @Override
    public void initData() {
        Log.d(TAG, "==> HomePager initData");
        super.initData();
        // 初始化数据
        String now = DateTimeUtil.getNowStr();
        List<Article> articleList = ArticleMapper.getArticleByTime(context, now);
        homeArticleAdapter = new HomeArticleAdapter(context, articleList);
        homeArticleAdapter.setOnItemClickListener((createTime) -> {
            changeLayoutManager(arrow, createTime);
            setDayText(date, day, DateTimeUtil.getLocalDate(createTime));
            change2Today(toToday, weather, createTime);
        });
        recyclerView.setAdapter(homeArticleAdapter);
        // 默认使用线性布局管理器
        recyclerView.setLayoutManager(linearLayoutManager);
    }
}
